﻿using Microsoft.AspNetCore.Components;
using SageKing.Designer.Core.Models;
using SageKing.Designer.Core.UI.Models;
using SageKing.Studio.Pages.Dashboard.Components;
using System.Text.Json.Nodes;

namespace SageKing.Studio.Pages.Dashboard;

public partial class Index
{
    private readonly DragDropManager _dragDropManager = new();
    private WorkflowDefinition? _workflowDefinition;
    private WorkflowDefinitionWorkspace WorkflowDefinitionWorkspace { get; set; } = default!;

    /// The ID of the workflow definition to load.
    [Parameter] public string DefinitionId { get; set; } = "0";

    /// Gets or sets the event that occurs when the workflow definition version is updated.
    [Parameter] public EventCallback<WorkflowDefinition> WorkflowDefinitionVersionSelected { get; set; }

    /// Gets or sets the event triggered when an activity is selected.
    [Parameter] public EventCallback<JsonObject> ActivitySelected { get; set; }

    /// Gets the currently selected workflow definition version.
    public WorkflowDefinition? GetSelectedWorkflowDefinitionVersion() => WorkflowDefinitionWorkspace.GetSelectedWorkflowDefinitionVersion();

    /// <summary>An event that is invoked when a workflow definition has been executed.</summary>
    /// <remarks>The ID of the workflow instance is provided as the value to the event callback.</remarks>
    [Parameter] public EventCallback<string> WorkflowDefinitionExecuted { get; set; }

    /// Gets the currently selected activity ID.
    public string? SelectedActivityId => WorkflowDefinitionWorkspace.SelectedActivityId;

    /// Gets a value that indicates if the workflow definition editor should be displayed in readonly mode.
    private bool IsReadOnly => false;

    #region init
    #region menu

    public IMenuTab<SysMenu, long> MeTab { get { return this; } }

    public IBaseServiceCache<SysMenu, long> MenuService
    { get { return _sysMenuService; } }

    [SupplyParameterFromQuery(Name = "menuid")]
    public long menuid { get; set; }

    [Parameter]
    public string TabTitle { get; set; } = "消息结构定义";

    [Parameter]
    public string TabIcon { get; set; } = "home";

 
    #endregion

    [CascadingParameter]
    public Error? Error { get; set; }

    #endregion

    protected override async Task OnInitializedAsync()
    {
        try
        {
            await MeTab.InitTabTitle();

        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }
    }
    /// <inheritdoc />
    protected override async Task OnParametersSetAsync()
    {
        try
        {
            _workflowDefinition = await WorkflowDefinitionService.FindByDefinitionIdAsync(DefinitionId, VersionOptions.Latest);
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }
    }
}
